﻿using UnityEngine;
using System.Collections;

namespace FrenzyIsland.UI
{
    public class UIMessageBox : MonoBehaviour
    {
        public string Message {
            get
            {
                if (string.IsNullOrEmpty(_message)) return "";
                return _message;
            }
            set
            {
                _message = value;
            }
        }
        private string _message;

        public GuiManager.SimpleDelegate OkDelegate { get; set; }
        public GuiManager.SimpleDelegate CancelDelegate { get; set; }
        
        public virtual void Start()
        {
            this.gameObject.name = "MsgBox";
        }
        
        public void btnOk_Click()
        {
            if (OkDelegate != null) OkDelegate();
            NGUITools.Destroy(this.gameObject);
        }

        public void btnCancel_Click()
        {
            if (CancelDelegate != null) CancelDelegate();
            NGUITools.Destroy(this.gameObject);
        }

        /// <summary>
        /// Utility per creare una sprite all'interno di una grid a sua volta dentro un UIWidget.
        /// La sprite risultato sarà quadrata di altezza container della grid
        /// </summary>
        public UISprite CreateSprite(GameObject grid, UIAtlas atlas, string spriteName, int height)
        {
            GameObject gamesprite = new GameObject("sprite");
            UISprite uisprite = gamesprite.AddComponent<UISprite>();

            uisprite.atlas = atlas;
            uisprite.spriteName = spriteName;
            uisprite.GetComponent<UIWidget>().height = height;
            uisprite.GetComponent<UIWidget>().width = height;
            uisprite.GetComponent<UIWidget>().depth = 5;

            gamesprite.transform.parent = grid.transform;
            gamesprite.transform.localPosition = Vector3.zero;
            gamesprite.transform.localScale = Vector3.one;

            return gamesprite.GetComponent<UISprite>();
        }

        public UILabel CreateLabel(GameObject grid, UIFont font, string text, int height, int? width, NGUIText.Alignment allignment = NGUIText.Alignment.Left)
        {
            GameObject objectlabel = new GameObject("label");

            UILabel label = objectlabel.AddComponent<UILabel>();
            label.text = text;
            label.bitmapFont = font;
            label.fontSize = 30;
            label.GetComponent<UIWidget>().height = height;
            label.GetComponent<UIWidget>().width = width == null ? height : (int)width;
            label.GetComponent<UIWidget>().depth = 5;
            label.alignment = allignment;

            objectlabel.transform.parent = grid.transform;
            objectlabel.transform.localPosition = Vector3.zero;
            objectlabel.transform.localScale = Vector3.one;

            return label;
        }

    }
}